/*
 * @Author: ckk
 * @Date: 2022-08-22 17:00:53
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-08-22 19:17:23
 * @Description: 公用方法
 */
const ossDomain = window.location.origin;
// const ossDomain = 'https://cs.yjt361.com';
/**
 * @description: 获取url的参数
 * @param {*} name
 * @Author: ckk
 * @Date: 2022-08-22 17:01:27
 * @return {*}
 */
 export function getUrlParam (name) {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
    r = window.location.search.substr(1).match(reg);
    if (r !== null) return unescape(r[2]);
    return null;
}
export function getParam (url, name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
  r = url.substr(1).match(reg);
  if (r !== null) return unescape(r[2]);
  return null;
}
/**
 * @description: 获取返回值中的参数
 * @param {*} url
 * @param {*} name
 * @Author: ckk
 * @Date: 2022-08-22 17:07:57
 * @return {*}
 */
export function getParameter (url, name) {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
    r = url.split('?')[1].match(reg);
    if (r !== null) return unescape(r[2]);
    return null;
}
/**
 * @description: 获取图片url链接
 * @param {*} posterUrl
 * @Author: ckk
 * @Date: 2022-08-24 15:55:57
 * @return {*}
 */
export function getImageUrl (posterUrl) {
    var resultposterUrl = '';
    if (posterUrl.indexOf('http://') === -1 && posterUrl.indexOf('https://') === -1) {
       resultposterUrl = ossDomain + posterUrl;
    }
    return resultposterUrl;
}
/**
 * @description: 跳转链接处理
 * @param {*} url
 * @param {*} type
 * @Author: ckk
 * @Date: 2022-08-25 19:17:43
 * @return {*}
 */
export function getLink (url) {
    if (url) {
        if (url.indexOf('vcom-area://h5') !== -1) {
            return url.split('?')[0].split('vcom-area://h5')[1];
        } else if (url.indexOf('vcom-area://portal_url') !== -1) {
            return url.replace((new RegExp('vcom-area://portal_url', 'g')), ossDomain);
        } else if (url.split('&')[0].split('type=')[1]) {
            return url.split('&')[0].split('type=')[1];
        } else if (url.indexOf('vcom://') !== -1) {
            return '';
        } else if (typeofType(url)) {
            if (JSON.parse(url).mobileUrl) {
                return ossDomain + JSON.parse(url).mobileUrl;
            } else {
                return '';
            }
        } else {
            return url;
        }
    } else {
      return '';
    }
}
// 判断的是否是JSON字符串
export function typeofType (str) {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str);
        if (typeof obj === 'object' && obj) {
            return true;
          } else {
            return false;
          }
        } catch (e) {
          return false;
        }
      }
  return false;
}
/**
 * @description: 获取json转换的本地存储
 * @param {*} name
 * @Author: ckk
 * @Date: 2022-08-26 15:25:40
 * @return {*}
 */
export function getJsonlocalStore (name) {
    const resultData = localStorage.getItem(name);
    if (resultData) {
        return JSON.parse(resultData);
    } else {
        return '';
    }
}
export function getSwiper (data, params) {
    const newTime = new Date().getTime(),
    termOfValidity = data.adList.filter(item => newTime >= new Date(item.onlineTime).getTime() && newTime <= new Date(item.offlineTime).getTime()),
    termOfValidity1 = [];
    termOfValidity.forEach(item => {
        const platformsLen = item.attributeList.filter(item1 => item1.code === params.platforms && item1.type === 1).length, // 筛选运营区域
        gradeCodesLen = item.attributeList.filter(item1 => item1.code === params.gradeCodes && item1.type === 2).length,
        typeFour = item.attributeList.filter(item1 => item1.type === 4).length,
        typeFive = item.attributeList.filter(item1 => item1.type === 5).length,
        schoolIdLen = item.attributeList.filter(item1 => item1.code === params.schoolIds && item1.type === 4).length,
        areaIdLen = item.attributeList.filter(item1 => item1.code === params.areaCodes && item1.type === 5).length;
        if (typeFour > 0 && typeFive > 0) {
            if (platformsLen > 0 && gradeCodesLen > 0 && schoolIdLen > 0 && areaIdLen > 0) {
                termOfValidity1.push(item);
            }
        } else if (typeFour > 0) {
            if (platformsLen > 0 && gradeCodesLen > 0 && schoolIdLen > 0) {
                termOfValidity1.push(item);
            }
        } else if (typeFive > 0) {
            if (platformsLen > 0 && gradeCodesLen > 0 && areaIdLen > 0) {
                termOfValidity1.push(item);
            }
        } else {
            if (platformsLen > 0 && gradeCodesLen > 0) {
                termOfValidity1.push(item);
            }
        }
    });
    if (termOfValidity1.length === 0) {
        return termOfValidity.filter(item => item.mainAd === 1 || item.mainAd === '1');
    } else {
        return termOfValidity1;
    }
}
/**
 * @description: 判断是否显示VIP图标/单品图标/体验图标
 * @param {*} resVips
 * @Author: zlw
 * @Date: 2023-08-25 10:56:40
 * @return {*}
 */
export function getVipStyle (resVips, id) {
    var showVips = {
        isVipShow: false,
        isProductShow: false,
        isExperienceShow: false
    }; // isVipShow显示VIP图标  isProductShow显示单品图标  isExperienceShow显示体验图标
    if (resVips.length <= 0) {
        return showVips;
    }
    var vips = resVips.map((item) => Number(item)), permitSell = localStorage.getItem('permitSell');
    if (vips.length > 0) {
        if (vips.includes(4)) {
            var courseIds = localStorage.getItem('courseIds');
            if (!courseIds.includes(id)) {
                showVips.isProductShow = true;
                showVips.isVipShow = false;
                showVips.isExperienceShow = false;
            } else {
                showVips.isProductShow = false;
                showVips.isVipShow = false;
                showVips.isExperienceShow = false;
            }
            if (permitSell === 5) {
                showVips.isProductShow = false;
                showVips.isExperienceShow = true;
                showVips.isVipShow = false;
            }
            return showVips;
        } else {
            var useVips = localStorage.getItem('useVips'),
                newArr = Array.from(new Set([...useVips].filter(x => vips.includes(x))));
            if (newArr.length === 0) {
                showVips.isProductShow = false;
                showVips.isVipShow = true;
                showVips.isExperienceShow = false;
            }
            if (permitSell === 5) {
                showVips.isProductShow = false;
                showVips.isExperienceShow = true;
                showVips.isVipShow = false;
            }
            if (newArr.length !== 0) {
                showVips.isProductShow = false;
                showVips.isVipShow = false;
                showVips.isExperienceShow = false;
            }
            return showVips;
        }
    } else {
        return showVips;
    }
}
